﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Utilities
{
    public class Utility
    {
        public static string ErrorTitle = "Error";
        public static string ExceptionTitle = "Exception";
        public static string InformTitle = "Inform";
        public static string WarningTitle = "Warning";

        public static string DefaultSymmetricAlgoName = "TripleDES";
        public static string DefaultHashAlgoName = "SHA1";

        public static int AESBlockSize = 192;
        public static string XmlUserFilePath = ".\\users.xml";
        public static HashAlgorithm HashAlgo = new SHA1Managed();

        static public string FromBytesToHexString(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder(bytes.Length * 2);
            foreach (byte b in bytes)
            {
                sb.AppendFormat("{0:x2}", b);
            }
            return sb.ToString();
        }

        static public byte[] FromHexStringToBytes(string str)
        {
            byte[] bytes = new byte[str.Length / 2];
            for (int i = 0; i < str.Length / 2; i++)
            {
                int high = int.Parse(str[2 * i].ToString(), System.Globalization.NumberStyles.HexNumber);
                int low = int.Parse(str[2 * i + 1].ToString(), System.Globalization.NumberStyles.HexNumber);
                bytes[i] = (byte)((high << 4) + low);
            }
            return bytes;
        }

        //Write data to file
        //return "" if success
        static public string WriteFile(string fileName, byte[] fileData)
        {
            FileStream fs = null;
            BufferedStream bf = null;
            try
            {
                fs = new FileStream(fileName, FileMode.OpenOrCreate);
                bf = new BufferedStream(fs);

                bf.Write(fileData, 0, fileData.Length);

                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
                if (bf != null)
                    bf.Close();
            }
        }

        static public string WriteFile(string fileName, string fileData)
        {
            FileStream fs = null;
            StreamWriter bf = null;
            try
            {
                fs = new FileStream(fileName, FileMode.OpenOrCreate);
                bf = new StreamWriter(fs);

                bf.Write(fileData);

                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                if (bf != null)
                    bf.Close();
                if (fs != null)
                    fs.Close();
            }
        }

        //read file
        //return content of file in string
        static public string ReadFile(string fileName)
        {
            FileStream fs = null;
            StreamReader bf = null;
            try
            {
                fs = new FileStream(fileName, FileMode.Open);
                bf = new StreamReader(fs);

                return bf.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (bf != null)
                    bf.Close();
                if (fs != null)
                    fs.Close();
            }
        }
    }
}
